package com.baiqi.rabbitmq.rabbitmq.helloworld;

import com.baiqi.rabbitmq.rabbitmq.utils.RabbitConstant;
import com.baiqi.rabbitmq.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitUtils.getConnection();

        Channel channel = connection.createChannel();
        channel.queueDeclare(RabbitConstant.QUEUE_HELLOWORLD,false,false,false,null);

        channel.basicConsume(RabbitConstant.QUEUE_HELLOWORLD,false,new Receiver(channel));
    }
    static class Receiver extends DefaultConsumer {
        private Channel channel;
        public Receiver(Channel channel) {
            super(channel);
            this.channel=channel;
        }

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            String message=new String(body);
            System.out.println("消费者收到的消息："+message);
            System.out.println("消息的TagId: "+envelope.getDeliveryTag());

            channel.basicAck(envelope.getDeliveryTag(),false);
        }
    }
}
